package com.syni.mdd.yhd.common.all.service;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsUserComplainDao;
import com.syni.mdd.yhd.common.all.entity.BmsUserComplain;
import com.syni.mdd.yhd.common.all.entity.BmsUserVideo;
import com.syni.mdd.yhd.common.all.entity.BmsVideo;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Optional;

/**
 * @className BmsUserOperationLogService
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsUserComplainService extends BaseService<BmsUserComplain, Integer> {

    @Autowired
    BmsUserComplainDao bmsUserComplainDao;

    @Autowired
    BmsUserVideoService bmsUserVideoService;

    @Autowired
    BmsVideoService bmsVideoService;


    @Override
    public BmsUserComplainDao getBaseDao() {
        return bmsUserComplainDao;
    }


    public Map<String,Object> complain(Integer userId,Integer videoId,
                                       Integer complainType,String content,Integer createWay){
        final int MAX_CONENT = 500;
        Optional<BmsVideo> bmsVideoOp = bmsVideoService.getBaseDao().findById(videoId);
        if(!bmsVideoOp.isPresent()){
            return ResultMapHelper.result(ResultCode.CODE_VIDEO_NO_EXIST,"视频不存在");
        }
        if(content.length() > MAX_CONENT){
            return ResultMapHelper.result(ResultCode.CODE_VIDEO_LONG_REPORT_TEXT,"举报内容过长");
        }
        BmsVideo bmsVideo = bmsVideoOp.get();
        BmsUserComplain bmsUserComplain = new BmsUserComplain();
        bmsUserComplain.setInformerId(userId);
        bmsUserComplain.setBmsVideoId(videoId);
        bmsUserComplain.setComplainType(complainType);
        bmsUserComplain.setContent(content);
        bmsUserComplain.setState(Integer.parseInt(Constants.COMMON_FLAG_FALSE));
        bmsUserComplain.setNewTime(new Date());
        bmsUserComplain.setCreateWay(createWay);
        bmsUserComplain.setVendorName(bmsVideo.getVendorName());
        bmsUserComplain.setFromOrigin(bmsVideo.getFromOrigin());
        bmsUserComplainDao.save(bmsUserComplain);
        return ResultMapHelper.success(ResultMsg.HAND_SUCCESS);
    }
}
